package dev.ece.core.cells.world;

import org.lwjgl.opengl.GL20;

public class ShadowInfoLocationHandler {

	private int shadowEnableHandler;
	
	private int shadowMappingHandler;
	
	private int shadowMatrixHandler;
	
	private int lightIndexHandler;

	private ShadowInfoLocationHandler() {}
	
	public static ShadowInfoLocationHandler newInstance(int program, String prefix) {
		ShadowInfoLocationHandler handler = new ShadowInfoLocationHandler();
		handler.shadowEnableHandler = GL20.glGetUniformLocation(program, prefix.concat("enable"));
		handler.shadowMappingHandler = GL20.glGetUniformLocation(program, prefix.concat("shadowMapping"));
		handler.shadowMatrixHandler = GL20.glGetUniformLocation(program, prefix.concat("shadowMatrix"));
		handler.lightIndexHandler = GL20.glGetUniformLocation(program, prefix.concat("lightIndex"));
		return handler;
	}

	public void glUniformShadowInfo(ShadowInfo shadowInfo, int index) {
		GL20.glUniform1i(this.shadowEnableHandler, shadowInfo.isEnable() ? 1 : 0);
		if(shadowInfo.isEnable()) {
			GL20.glUniform1i(this.shadowMappingHandler, shadowInfo.getShadowMapping().bind(index));
			GL20.glUniform1i(this.lightIndexHandler, shadowInfo.getLightIndex());
			GL20.glUniformMatrix4fv(this.shadowMatrixHandler, false, shadowInfo.getShadowMatrixBuffer());
		}
	}
	public int getShadowEnableHandler() {
		return shadowEnableHandler;
	}

	public int getShadowMappingHandler() {
		return shadowMappingHandler;
	}

	public int getShadowMatrixHandler() {
		return shadowMatrixHandler;
	}
	
	public void clearup() {
		GL20.glDeleteShader(shadowEnableHandler);
		GL20.glDeleteShader(shadowMappingHandler);
		GL20.glDeleteShader(shadowMatrixHandler);
		GL20.glDeleteShader(lightIndexHandler);
	}
	
}
